﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SharpDiary.Core.Controls
{
    public partial class ListBoxForDTB : UserControl
    {
        private DateValueType _valueType = DateValueType.Year;
        private List<DateValue> _items = new List<DateValue>();

        public event EventHandler<PressedEventArgs> Pressed;

        protected virtual void OnPressed(object sender, PressedEventArgs e)
        {
            if (Pressed != null) { Pressed(sender, e); }
        }

        public ListBoxForDTB()
        {
            InitializeComponent();

            ResizeRedraw = true;
            DoubleBuffered = true;
        }

        public void DisplayYears(List<int> years)
        {
            _valueType = DateValueType.Year;

            listBoxMain.Items.Clear();
            _items.Clear();

            foreach (int year in years)
            {
                DateValue item = new DateValue(DateValueType.Year, year);
                _items.Add(item);
                listBoxMain.Items.Add(item);
            }

            if (_items.Count > 0)
            {
                listBoxMain.Focus();
                listBoxMain.SelectedIndex = 0;
            }
        }

        public void DisplayMonths()
        {
            _valueType = DateValueType.Month;

            listBoxMain.Items.Clear();
            _items.Clear();

            for (int i = 1; i <= 12; i++)
            {
                DateValue item = new DateValue(DateValueType.Month, i);
                _items.Add(item);
                listBoxMain.Items.Add(item);
            }

            for (int i = 1; i <= 12; i++)
            {
                DateValue item = new DateValue(DateValueType.Month, i);
                _items.Add(item);
                listBoxMain.Items.Add(item);
            }

            if (_items.Count > 0)
            {
                listBoxMain.Focus();
                listBoxMain.SelectedIndex = 0;
            }
        }

        public void DisplayDays(int year, int month)
        {
            _valueType = DateValueType.Day;

            listBoxMain.Items.Clear();
            _items.Clear();

            int days = DateTime.DaysInMonth(year, month);
            for (int i = 1; i <= days; i++)
            {
                DateValue item = new DateValue(DateValueType.Day, i);
                _items.Add(item);
                listBoxMain.Items.Add(item);
            }

            if (_items.Count > 0)
            {
                listBoxMain.Focus();
                listBoxMain.SelectedIndex = 0;
            }
        }

        private void listBoxMain_MouseClick(object sender, MouseEventArgs e)
        {
            PressedEventArgs args = new PressedEventArgs();
            args.SelectedValue = listBoxMain.SelectedItem as DateValue;
            OnPressed(this, args);
        }

        private void listBoxMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PressedEventArgs args = new PressedEventArgs();
                args.SelectedValue = listBoxMain.SelectedItem as DateValue;
                OnPressed(this, args);
            }
        }
    }

    public class PressedEventArgs : EventArgs
    {
        private DateValue _selectedValue;

        public DateValue SelectedValue
        {
            get { return _selectedValue; }
            set { _selectedValue = value; }
        }

        public PressedEventArgs()
        {

        }
    }

    public enum DateValueType { Year, Month, Day }

    public class DateValue
    {
        private DateValueType _valueType = DateValueType.Year;

        public DateValueType ValueType
        {
            get { return _valueType; }
            set { _valueType = value; }
        }

        private int _value = 0;

        public int Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public DateValue() { }

        public DateValue(DateValueType valueType, int value)
        {
            _valueType = valueType;
            _value = value;
        }

        public override string ToString()
        {
            switch (_valueType)
            {
                case DateValueType.Year: return string.Format("{0}年", _value);
                case DateValueType.Month: return string.Format("{0}月", _value);
                case DateValueType.Day: return string.Format("{0}日", _value);
                default: return string.Empty;
            }
        }
    }
}
